<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseApiController;
use App\Services\Admin\UserService;

/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2019/2/19
 * Time: 16:42
 */
class UserController extends BaseApiController
{
    private $service;

    public function __construct(UserService $service)
    {
        //继承基类的构造方法
        parent::__construct();
        //依赖注入service
        $this->service=$service;
    }
    public function add(){
        //写一些验证规则
        //验证参数
        //controller 去service调用逻辑代码
        //封装一个
        echo 1;die;
    }

    /**
     * 查询用户信息单条
     */
    public function sel(Request $Request){
        $data['id']=$Request->input('id');
        $rules=['id' => "required|integer|max:10"];
        $errmessge=['id.required'=>"id不能为空"];
        $arr=$this->service->UserOne($data,$rules,$errmessge);
        return json_encode($arr);
        // response()->json();
    }

    // 修改用户信息
    public function save(Request $request){
        $rules=[
            'id' => 'required|integer',
            'u_head_picture' => 'required|image',
            'u_name' => 'required|max:50',
            'u_sex' => 'required',
            'u_school' => 'required|max:50',
            'u_major' => 'required|max:30',
            'u_education' => 'required|max:50',
            'u_graduation_date' => 'required|date',
            'u_mailbox' => 'required|email'
        ];
        $errmessge=[
            'id.required'=>"id不能为空",
            'id.integer'=>"id必须是整数",
            'u_head_picture.required'=>"头像不能为空",
            'u_head_picture.image'=>"头像必须是图片",
            'u_sex'=>"性别不能为空",
            'u_school.required'=>"学校不能为空",
            'u_school.max'=>"长度限制50个字符",
            'u_major.required'=>"职位不能为空",
            'u_major.max'=>"长度限制40个字符",
            'u_education.required'=>"学历不能为空",
            'u_education.max'=>"长度限制50个字符",
            'u_graduation_date.required'=>"日期不能为空",
            'u_graduation_date.date'=>"日期格式为****-**-**",
            'u_mailbox.required'=>"邮箱不能为空",
            'u_mailbox.email'=>"邮箱格式不正确",
            ];
        $data=$request->all();
        return $this->service->ServiceSave($data,$rules,$errmessge);
    }

    /**
     * @param Request $Request
     *修改用户密码
     */
     public function uppwd(Request $Request)
     {
         $data=$Request->all();
         $rules=[
             'id' => 'required|integer',
             'password'=>'required',
             'repassword'=>['required',"same:password"//不为空,两次密码是否相同
             ],
         ];
         $messages=[
             'id.required'=>"id不能为空",
             'id.integer'=>"id必须是整数",
             'password.required'=>"密码不能为空",
             'repassword.required'=>"确认密码不能为空",
             'repassword.same'=>'密码与确认密码不匹配',
         ];
        return    $this->service->uppwd($data,$rules,$messages);
     }

    /**兑换商品 展示商品列表 并返回用户个人积分
     * @param Request $Request
     * @return array
     */
     public function GoodslistUserintegral(Request $Request)
     {
         $data['id']=$Request->input('id');
         $rules=['id' => "required|integer|max:10"];
         $errmessge=['id.required'=>"id不能为空"];
         return $this->service->GoodslistUserintegral($data,$rules,$errmessge);
     }

    /**
     * 查询用户积分日志表
     */
    public function integrallogs(Request $Request){
        $data['id']=$Request->get('id');
        $rules=['id' => "required|integer|max:10"];
        $errmessge=['id.required'=>"id不能为空"];
        $data=$this->service->UserIntegralog($data,$rules,$errmessge);
        return $data;
    }

    /**
     * 用户兑换商品记录表 和积分
     */

    public function egoodslog(Request $Request){
        $data['id']=$Request->get('id');
        $rules=['id' => "required|integer|max:10"];
        $errmessge=['id.required'=>"id不能为空"];
        $data=$this->service->egoodslog($data,$rules,$errmessge);
        return $data;
    }

    /**
     * 查询所有商品
     * @param Request $Request
     * @return mixed
     */
    public function goodsall(Request $Request)
    {
        $p=$Request->get('p');  //页码
        return $this->service->goodsall($p);
    }

    /**
     * 返回商品详情
     * @param Request $Request
     * @return mixed
     */
    public function goodone(Request $Request)
    {
       return $this->service->goodone($Request->get('id'));
    }

    /**
     * 积分兑换商品
     * @param Request $request
     * @return array|string
     */
    public function exchangegood(Request $request)
    {
        // 用户姓名 手机号 地区 详细地址
        $uid=$request->get('uid');
        $gid=$request->get('gid');
        $num=$request->get('num');
        $addr_name=$request->get('addr_name');
        $addr_phone=$request->get('addr_phone');
        $addr_address=$request->get('addr_address');
        $addr_detail=$request->get('addr_detail');

        return $this->service->integralexchangegood($gid,$uid,$num,$addr_name,$addr_phone,$addr_address,$addr_detail);
    }

    //没有删除的不用补全
    public function del(){

    }


}